﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnnamedPodcastLibrary
{
    internal class PriorityQueue<TElement, TPriority>
    {
        readonly SortedDictionary<TPriority, Queue<TElement>> _container = new SortedDictionary<TPriority, Queue<TElement>>();
        public TElement Dequeue()
        {
            foreach(var queue in this._container.Values)
            {
                if(queue.Count > 0)
                    return queue.Dequeue();
            }
            return default(TElement);
        }

        public Int32 Count
        {
            get
            {
                return this._container.Values.Sum((queue) => { return queue.Count; });
            }
        }

        public void Enqueue(TElement element, TPriority priority)
        {
            Queue<TElement> queue;
            if (!this._container.TryGetValue(priority, out queue))
            {
                this._container.Add(priority, new Queue<TElement>());
                queue = this._container[priority];
            }
            if(!queue.Contains(element))
                queue.Enqueue(element);
        }
    }
}
